package com.jic.interaction.ChinaPost.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.interaction.ChinaPost.common.exception.UserInfoException;
import com.jic.interaction.ChinaPost.common.menu.UserInfoEnums;
import com.jic.interaction.ChinaPost.service.UserInfoService;
//import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.ChinaPost.model.entity.UserInfo;
import com.jic.member.api.rest.MememberApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * @ClassName UserInfoServiceImpl
 * @Description TODO
 * @Author 陈鸿锋
 * @Date 2020/11/5 14:30
 * @Version 1.0
 **/
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Value("${route.value}")
    private String url;

    @Value("${wz.appid}")
    private String appId;

    @Resource
    private MememberApi mememberApi;

    private final String queryAccountInfoUrl = url+"/m/dbs-front/user/queryAccountInfo";
    /*
     * @Author chengpunan
     *@Package com.jic.interaction.service.impl
     *ClassName UserInfoServiceImpl
     * @Description  //TODO  用户信息查询
     * @Date 11:29 2020/11/10
     * @Param userInfo
     * @return UserInfo
     * @Version:1.0
     **/
    @Override
    public UserInfo getUserInfo(UserInfo userInfo) throws InterruptedException, ExecutionException, IOException {
        log.info("用户信息查询参数入参，{}", JSONObject.toJSONString(userInfo));
        String url=this.url+"/m/dbs-front/user/queryAccountInfo";
        //业务参数
        userInfo.setAppId(appId);
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(userInfo);
        String param= HttpClientUtil.requestPath(url, null,jsonObject.toString());
        com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(param);
        if (jsonObject1==null || !jsonObject1.containsKey("code")) {
           throw new UserInfoException(UserInfoEnums.RETURN_NOT_NULL);
        }
        if (!jsonObject1.getString("code").equals("0")) {
            RestResult.error(jsonObject1.getString("code"),jsonObject1.getString("msg"));
            throw new UserInfoException(jsonObject1.getString("code"),jsonObject1.getString("msg"));

        }
        UserInfo userInfo1 = new UserInfo();
//        userInfo1.setAppId(jsonObject1.getString("appId"));
        userInfo1.setUserId(jsonObject1.getString("userId"));
        userInfo1.setBalance(jsonObject1.getBigDecimal("balance"));
        userInfo1.setThreeActStatus(jsonObject1.getString("threeActStatus"));
        userInfo1.setSbActStatus(jsonObject1.getString("sbActStatus"));
        log.info("用户信息查询结束");
        return userInfo1;
    }
}
